/*
twTablePager v.2.6

- no inner ajax load
- require jquery UI framework
- by tiger

sample : $("#gridHolder").twTablePager({maxPage : 10, onPaging:function(pageNumber){ loadTableFunction(pageNumber) }	});

*/
(function($) {

	jQuery.fn.twTablePager = function(settings) {
		
		var defaults = { 
						ajaxURL : null, //sample "ListView?page="
						caption : "Page : ",
						firstTitle : "Go to first page",
						lastTitle : "Go to last page",
						prevTitle : "Go to previous page",
						nextTitle : "Go to next page",
						currentPage : 1,
						maxPage : 10,
						rangePage : 10,
						currentPageTitle : "CurrentPage :",
						showCurrentpage :true,
						maxPageTitle : "TotalPage :",
						showMaxpage : true,
						onPaging : function(){return true}
						};
						
		var options = jQuery.extend(defaults, settings);
		var holder = $(this);
		var currentPage = options.currentPage;
		if(currentPage == null){currentPage=1}
		//load ajaxURL to Holder area
		$("div[pagingOf="+$(holder).attr("id ")+"]").remove();
		var maxpage = options.showMaxpage;
		var currentpage = options.showCurrentpage;
			//set paging width
	if(options.maxPage > 1){
			//add paging Markup
			$(this).after('<div class="paging ui-state-highlight ui-corner-bottom ui-helper-clearfix" style=" padding:2px; width:'+($(this).width()-4)+'px"><span style="float:left">'+options.caption+'</span><span class="firstPage ui-icon ui-icon-seek-first" style="cursor:pointer;float:left" title="'+options.firstTitle+'"></span><span class="prevPage ui-icon ui-icon-seek-prev" style="cursor:pointer; float:left; margin-left:5px" title="'+options.prevTitle+'"></span><div class="pageNum" style="float:left"></div><span class="nextPage ui-icon ui-icon-seek-next" style="cursor:pointer; float:left; margin-left:5px" title="'+options.nextTitle+'"></span><span class="lastPage ui-icon ui-icon-seek-end" style="cursor:pointer; float:left; margin-left:5px" title="'+options.lastTitle+'"></span><div class="CurrentPage" style="float:left; padding-left:10px"></div><div class="MaxPage" style="float:right; padding-right:10px"></div></div>');
			
			//add page number
			var divpaging = $("div[pagingOf="+$(holder).attr("id ")+"]");
			var ceilrangePage = Math.ceil(options.rangePage/2)
			var floorrangePage = Math.floor(options.rangePage/2)
			var pagelastMax = options.maxPage-floorrangePage
			var floorR = options.maxPage-floorrangePage
			var t1 = options.maxPage-(options.rangePage-1)
			
			
			if(options.maxPage>options.rangePage){
				if(currentPage<=ceilrangePage){
					for(i=1;i<=options.rangePage;i++){
						if(i == currentPage) { thisWeight = ";font-weight:bold" }else{ thisWeight = ";font-weight:normal" }
						$(holder).next("div.paging").find(".pageNum").append('<span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px'+thisWeight+'">'+i+'</span>');
						$(holder).next("div.paging").find(".pageNum span:last").click(function(){
							var thisNum = $(this);
							var thisPage = $(this).text();
							gotoPage(thisNum,thisPage,"pageNum");
						});
					}
						$(holder).next("div.paging").find(".pageNum").append('<span class="point" style="float:left; margin-left:5px'+thisWeight+'">...</span>');
				}else if(ceilrangePage<currentPage&&currentPage<=floorR){
					var f1 = Number(currentPage)-floorrangePage
					var f2 = Number(currentPage)+floorrangePage
					var f3 = options.rangePage%2
					if(f3==0){
						$(holder).next("div.paging").find(".pageNum").append('<span style="float:left; margin-left:5px'+thisWeight+'">...</span>');
						for(i=f1;i<=f2-1;i++){
							if(i == currentPage) { thisWeight = ";font-weight:bold" }else{ thisWeight = ";font-weight:normal" }
								$(holder).next("div.paging").find(".pageNum").append('<span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px'+thisWeight+'">'+i+'</span>');
								$(holder).next("div.paging").find(".pageNum span:last").click(function(){
									var thisNum = $(this);
									var thisPage = $(this).text();
									gotoPage(thisNum,thisPage,"pageNum");
								});
						}
						$(holder).next("div.paging").find(".pageNum").append('<span style="float:left; margin-left:5px'+thisWeight+'">...</span>');
					}else{
						$(holder).next("div.paging").find(".pageNum").append('<span style="float:left; margin-left:5px'+thisWeight+'">...</span>');
						for(i=f1;i<=f2;i++){
							if(i == currentPage) { thisWeight = ";font-weight:bold" }else{ thisWeight = ";font-weight:normal" }
								$(holder).next("div.paging").find(".pageNum").append('<span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px'+thisWeight+'">'+i+'</span>');
								$(holder).next("div.paging").find(".pageNum span:last").click(function(){
									var thisNum = $(this);
									var thisPage = $(this).text();
									gotoPage(thisNum,thisPage,"pageNum");
								});
						}
						if(currentPage<pagelastMax){
							$(holder).next("div.paging").find(".pageNum").append('<span style="float:left; margin-left:5px'+thisWeight+'">...</span>');
						}
					}
				}else if(currentPage>floorR){
						$(holder).next("div.paging").find(".pageNum").append('<span style="float:left; margin-left:5px'+thisWeight+'">...</span>');
					for(i=t1;i<=options.maxPage;i++){
						if(i == currentPage) { thisWeight = ";font-weight:bold" }else{ thisWeight = ";font-weight:normal" }
							$(holder).next("div.paging").find(".pageNum").append('<span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px'+thisWeight+'">'+i+'</span>');
							$(holder).next("div.paging").find(".pageNum span:last").click(function(){
								var thisNum = $(this);
								var thisPage = $(this).text();
								gotoPage(thisNum,thisPage,"pageNum");
							});
					}
				}
				
			}else{
				for(i=1;i<=options.maxPage;i++){
					if(i == currentPage) { thisWeight = ";font-weight:bold" }else{ thisWeight = ";font-weight:normal" }
					$(holder).next("div.paging").find(".pageNum").append('<span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px'+thisWeight+'">'+i+'</span>');
					$(holder).next("div.paging").find(".pageNum span:last").click(function(){
						var thisNum = $(this);
						var thisPage = $(this).text();
						gotoPage(thisNum,thisPage,"pageNum");
					});
				}	
			}
			
			//CurrentPage
			if(currentpage==true){
			  $(holder).next("div.paging").find(".CurrentPage").append('<span style="float:left">'+options.currentPageTitle+'</span><span style="cursor:pointer;text-decoration:underline;float:left; margin-left:5px;">'+currentPage+'</span>')
			  
			  $(holder).next("div.paging").find(".CurrentPage span:last").click(function(){
				  var thisNum = $(this);
				  var thisPage = $(this).text();
				  gotoPage(thisNum,thisPage,"pageNum");
			  });
			}
			//end CurrentPage
			//show MaxPage
			if(maxpage==true){
				$(holder).next("div.paging").find(".MaxPage").append('<span>'+options.maxPageTitle+'</span><span style="cursor:pointer;text-decoration:underline; margin-left:5px;">'+options.maxPage+'</span>')
				
				$(holder).next("div.paging").find(".MaxPage span:last").click(function(){
					var thisNum = $(this);
					var thisPage = $(this).text();
					gotoPage(thisNum,thisPage,"pageNum");
				});
			}//end MaxPage
			
			
			$(holder).next("div.paging").find(".firstPage").click(function(){
				var thisNum = $(this);
				var thisPage = 1;
				gotoPage(thisNum,thisPage,"firstPage");
			});
			//go to last Page
			$(holder).next("div.paging").find(".lastPage").click(function(){
				var thisNum = $(this);
				var thisPage = options.maxPage;
				gotoPage(thisNum,thisPage,"lastPage");
			});
			//go to next Page
			$(holder).next("div.paging").find(".nextPage").click(function(){
				//alert("currentPage"+currentPage)
				var thisNum = $(this);
				var thisPage = Number(currentPage)+1;
				//alert("thisPage"+thisPage)
				if(currentPage < Number(options.maxPage)) { gotoPage(thisNum,thisPage,"nextPage") };
				//gotoPage(thisNum,thisPage,"nextPage")
			});
			//go to prev Page
			$(holder).next("div.paging").find(".prevPage").click(function(){
				var thisNum = $(this);
				var thisPage = Number(currentPage)-1;
				if(currentPage > 1) { gotoPage(thisNum,thisPage,"prevPage"); };
			});

		function gotoPage(obj,pageNum,displayType){
			if(options.onPaging(pageNum) == false ){return false}
			$(holder).html('<div style="padding:10px"><img src="'+CDNname+'images/loading.gif" alt="loading" align="absmiddle"/>Opening page please wait...</div>');
			$(divpaging).find(".pageNum span").css({'font-weight':'normal'});
				
			switch (displayType) {
	
				case 'firstPage':
					$(divpaging).find(".pageNum span:first").css({'font-weight':'bold'});
					break;
				
				case 'lastPage':
					$(divpaging).find(".pageNum span:last").css({'font-weight':'bold'});
					break;
				
				case 'nextPage':
					$(divpaging).find(".pageNum span:eq("+currentPage+")").css({'font-weight':'bold'});;
					break;
				
				case 'prevPage':
					$(divpaging).find(".pageNum span:eq("+(currentPage-2)+")").css({'font-weight':'bold'});
					break;
				
				default:
					$(obj).css({'font-weight':'bold'});
					break;
			}
			
			currentPage = pageNum;
				
		}
		
	}//end plug-in
	
}
	
	
	
})(jQuery);	